import base from '../../common/controller/api'

export default class extends base {
    async captchaAction() {
        const transpondConf = think.config('transpond');
        const Transpond = think.service('transpond');
        const transpond2Captcha = new Transpond(transpondConf.captcha);
        try {

            const rawRes = await transpond2Captcha.send({
                method: 'post',
                path: '/',
                params: {}
            }, null, true);

            if (!rawRes.ok) {
                throw new Error('验证码请求出错');
            }
            const signInfo = await this.session('signInfo') || {};
            // const validInfo = await this.session('validInfo') || {};

            //signInfo.validate = rawRes.headers.validate;
            signInfo.validate = rawRes.headers.get('validate');
            think.log(`captcha validate: ${signInfo.validate}`, 'LOG');

            await this.session('signInfo', signInfo);

            // 非 thinkjs 的 end 需手动调用内部接口。
            this.http._afterEnd();
            //console.log('this.http.res----------',this.http.res);
            rawRes.body.pipe(this.http.res);
        } catch (err) {
            const {
                errno = 1000, errmsg = '', info, errInfo = 'ERROR'
            } = err;
            think.log(err, errInfo);
            return this.fail(errno, errmsg, info);
        };

    }

    // 以下方法需要覆写
    /*async doAuth({
     auth,
     ...data
     }, signInfo) {
     const transpond = this.getTranspond();

     const {
     loginId,
     password: token
     } = data;
     auth = auth || `${loginId},${token}`;

     const resData = await transpond.send({
     method: 'get',
     path: `/user/${encodeURI(loginId)}/menus`
     }, auth);

     const mobile = resData.mobile;

     signInfo.token = auth;
     // signInfo.signFailCount = 0;

     return {
     mobile
     };
     }
     async getUserInfo({
     params: {
     mobile
     }
     }) {
     const {
     dataList
     } = await this.transWithLogin({
     method: 'get',
     path: `/user/${mobile}/vehicles`
     });

     return {
     mobile,
     vehicleList: dataList || []
     };
     }
     async signUp(data) {
     const {
     valmsg,
     params: {
     userType,
     ...params
     }
     } = data;
     const transpond = this.getTranspond();
     return transpond.send({
     method: 'POST',
     path: '/user',
     params: {
     userType: '4', // 普通用户
     ...params
     }
     }, `${data.params.mobile},${think.md5(valmsg).toUpperCase()}`); // 验证码需要大写在md5
     }
     async resetPassword(data) {
     const {
     valmsg,
     params
     } = data;
     const transpond = this.getTranspond();
     return transpond.send({
     method: 'PUT',
     path: `/user/${params.mobile}/self_password`,
     params: {
     newPassword: params.newpassword
     }
     }, `${params.mobile},${think.md5(valmsg).toUpperCase()}`); // 验证码需要大写在md5
     }
     async transData(data) {
     const {cmd, method} = data;
     const superRes = super.transData(data);

     // 特殊请求拦截
     switch (cmd) {
     case 'resetPassword':
     {
     //密码重置
     const resData = await this.resetPassword(data);
     return this.success(resData);
     }
     case 'signUp':
     {
     // 注册
     const resData = await this.signUp(data);
     return this.success(resData);
     }
     }

     return superRes;
     }
     needLogin(data) {
     return !['signNum'].includes(data.cmd);
     }
     getTranspond(transpondConf = think.config('transpond.rs')) {
     const Transpond = think.service('transpond');
     const transpond = new Transpond(transpondConf);

     return transpond;
     }*/
};